/// <reference path="sage.util.js" />
/// <reference path="sage.input.__.base.js" />

var KeyboardInput = BaseInputHandler.extend({
    initialize: function initialize() {
        BaseInputHandler.initialize.call(this, ["keydown.keyboard-input", "keyup.keyboard-input"]);
    },
    sendKeyDown: function sendKeyDown(context) {
        for (subId in this.subscribers) {
            this.subscribers[subId].fireEvent("keydown", context);
        }
    },
    sendKeyUp: function sendKeyUp(context) {
        for (subId in this.subscribers) {
            this.subscribers[subId].fireEvent("keyup", context);
        }
    },
    setup: function setup() {
        if (!this.enabled) {
            this.enabled = true;
            var self = this;
            $(window).on('keydown.keyboard-input', function (event) {
                var context = { prevent: false, which: event.which };
                self.sendKeyDown(context);
                if (context.prevent) event.preventDefault();
            });
            $(window).on('keyup.keyboard-input', function (event) {
                var context = { prevent: false, which: event.which };
                self.sendKeyUp(context);
                if (context.prevent) event.preventDefault();
            });
        }
    }
});